package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.pojo.CheckItem;
import cn.itcast.service.HospitalService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/address")
@RestController
@CrossOrigin
public class HospitalController {

    @Reference(version = "1.0.0")
    private HospitalService hospitalService;

    /**
     * 查询分院
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findPage() {
        List<Address> hospital = null;
        try {
            hospital = this.hospitalService.findHospital();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, hospital);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    /**
     * 添加分院
     *
     * @param address
     * @return
     */
    @PostMapping("/submit")
    public Result handleSubmit(@RequestBody Address address) {
        try {
            this.hospitalService.handleSubmit(address);
            return new Result(true, "新增分院成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增分院失败");
        }
    }

    /**
     * 修改分院
     *
     * @param
     * @return
     */
    @PutMapping("/update")
    public Result handleEdit(@RequestBody Address address) {
        try {
            this.hospitalService.update(address);
            return new Result(true, "修改分院成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改分院失败");
        }
    }

    /**
     * 根据id查询分院
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result updateData(@PathVariable Integer id) {
        try {
            Address address = this.hospitalService.selectById(id);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS, address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    @PutMapping("/{enable}/{id}")
    public Result yesOrNo(@PathVariable Integer enable,
                          @PathVariable Integer id) {
        try {
            Integer aaa = this.hospitalService.yesOrNo(enable, id);
            if (aaa == 1) {
                return new Result(true, "启用成功");
            }
            if (aaa == 0) {
                return new Result(true, "禁用成功");
            }
            return new Result(false, "无法连接到网络，请检查您的网络");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "未知错误");
        }
    }
}
